  

package pl.genapp.stocksense;

import java.util.Date;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.TimeChart;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import pl.genapp.appsense.R;
import pl.genapp.stocksense.libraries.HistoricalDataProvider;
import pl.genapp.stocksense.libraries.SentGraph;
import pl.genapp.stocksense.libraries.SentGraph.OnSentClickListener;
import pl.genapp.stocksense.libraries.SentGraphPie;
import pl.genapp.stocksense.others.PostExecuteReaction;
import pl.genapp.stocksense.others.TimeChartHelper;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
 
public class EntityScreenFinancial {
	
	private EntityScreenFragment parent;
	private View layout;
	private LinearLayout graphPlace;
	private SentGraphPie entityTwoSentGraph;
	private int[] sentLineColors = { 0xFFFF0000, 0xFFA523AA, 0xFF0000FF, 0xFF0FA40A, 0xFF1FA0F0, 0xFFAF501F };
	
	public EntityScreenFinancial(EntityScreenFragment parent) {
		this.parent = parent;
	}
	
	View setupView() {
		
		StackSense activity = (StackSense) parent.getActivity();
		
		if (activity != null) {
			
			layout = activity.getLayoutInflater().inflate(R.layout.fragment_entity_screen_two, null);
			
			graphPlace = (LinearLayout) layout.findViewById(R.id.entityTwoGraph);
			
			entityTwoSentGraph = (SentGraphPie) layout.findViewById(R.id.entityTwoSentGraph);
			entityTwoSentGraph.setOnSentClickListener(pieChartClickListener);
			entityTwoSentGraph.getLegendRendered().setCustomLabels(parent.getActivity().getResources().getStringArray(R.array.sentiments_name));
			entityTwoSentGraph.postInvalidate();
			
			return layout;
		} else
			return null;
		
	}
	
	PostExecuteReaction<List<HistoricalDataProvider.HistoricalDataField>> onGetFinancialChartData = new PostExecuteReaction<List<HistoricalDataProvider.HistoricalDataField>>() {
		
		public void onSuccess(Context context, java.util.List<pl.genapp.stocksense.libraries.HistoricalDataProvider.HistoricalDataField> result) {
			
			parent.setFinancialChartsData(result);
			parent.setFinancialChartsDataValid(true);
			
			loadFinancialGraph();
			
		};
		
	};
	
	void loadFinancialGraph() {
		
		// generowanie wykresu
		XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
		XYMultipleSeriesRenderer mRenderer = TimeChartHelper.initializeRendered(parent.getEntityName());
		
		XYSeriesRenderer financeLine = TimeChartHelper.initializeFinanceLine();
		mRenderer.addSeriesRenderer(financeLine);
		
		TimeSeries series = new TimeSeries("finance");
		for (int i = 0; i < parent.getFinancialChartsData().size(); i++) {
			series.add(parent.getFinancialChartsData().get(i).date, parent.getFinancialChartsData().get(i).adjClose);
		}
		mDataset.addSeries(series);
		
		if (parent.getActivity() != null) {
			
			GraphicalView mChartView = ChartFactory.getTimeChartView(parent.getActivity(), mDataset, mRenderer, "dd/MM");
			graphPlace.removeAllViewsInLayout();
			graphPlace.addView(mChartView);
			
		}
		
	}
	
	void loadFinancialAndSentimentGraph(final int sentNo) {
		
		double maxF = 0;
		double minF = 0;
		double maxS = 0;
		Double scale = 0d;
		
		// generowanie wykresu
		XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
		XYMultipleSeriesRenderer mRenderer = TimeChartHelper.initializeRendered(parent.getEntityName());
		
		XYSeriesRenderer financeLine = TimeChartHelper.initializeFinanceLine();
		mRenderer.addSeriesRenderer(financeLine);
		
		TimeSeries series = new TimeSeries("finance");
		for (int i = 0; i < parent.getFinancialChartsData().size(); i++) {
			series.add(parent.getFinancialChartsData().get(i).date, parent.getFinancialChartsData().get(i).adjClose);
		}
		mDataset.addSeries(series);
		
		// sentyment
		
		// Znajduje minimum i maximum w danych z yahoo
		if (!parent.getFinancialChartsData().isEmpty()) {
			maxF = parent.getFinancialChartsData().get(0).adjClose;
			minF = parent.getFinancialChartsData().get(0).adjClose;
			
			for (int i = 1; i < parent.getFinancialChartsData().size(); i++) {
				
				if (parent.getFinancialChartsData().get(i).adjClose > maxF) maxF = parent.getFinancialChartsData().get(i).adjClose;
				
				if (parent.getFinancialChartsData().get(i).adjClose < minF) minF = parent.getFinancialChartsData().get(i).adjClose;
				
			}
		}
		
		// Znajduje maximum z sentymentu
		if (!parent.getSentimentsChartsData()[sentNo].isEmpty()) {
			
			maxS = (Double) parent.getSentimentsChartsData()[sentNo].get(0);
			
			for (int i = 1; i < parent.getSentimentsChartsData()[sentNo].size(); i++) {
				
				if (((Double) parent.getSentimentsChartsData()[sentNo].get(i)) > maxS) {
					maxS = (Double) parent.getSentimentsChartsData()[sentNo].get(i);
				}
				
			}
			
		}
		
		if (maxS == 0) maxS = 1;
		scale = (maxF - minF) / maxS;
		
		XYSeriesRenderer sentLine = TimeChartHelper.initializeSentLine(sentLineColors[sentNo]);
		mRenderer.addSeriesRenderer(sentLine);
		TimeSeries sentSeries = new TimeSeries("sent");
		for (int i = 0; i < parent.getSentimentsChartsData()[sentNo].size(); i++) {
			sentSeries.add(new Date(parent.getFinancialChartsData().get(0).date.getTime() + i * TimeChart.DAY), (Double) parent.getSentimentsChartsData()[sentNo].get(i) * scale + minF);
		}
		mDataset.addSeries(sentSeries);
		
		// koniec sentyment
		GraphicalView mChartView = ChartFactory.getTimeChartView(parent.getActivity(), mDataset, mRenderer, "dd/MM");
		graphPlace.removeAllViews();
		graphPlace.addView(mChartView);
		
	}
	
	OnSentClickListener pieChartClickListener = new OnSentClickListener() {
		
		public void onSentClick(int sentNo) {
			
			Activity activity = parent.getActivity();
			if (activity != null) {
				loadFinancialAndSentimentGraph(sentNo);
			}
			
		}
	};
	
}
